package com.ust.diamond;

import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;

import libsvm.svm;
import libsvm.svm_model;
import libsvm.svm_node;

public class Diamond {
	private Point pos;
	private Point index;
	private int size = Data.SIZE_X;
	private int character;
	public int color;
//	public double color;
	private BufferedImage image;
	
	private static ArrayList<Integer> color_list = new ArrayList<Integer>(16);
	private static int img_index = 0;
	
	
//	public Diamond(int pos_x, int pos_y, int size, BufferedImage img) {
//		this.pos.x = pos_x;
//		this.pos.y = pos_y;
//		this.size = size;
//	}
	
	public Diamond(int x_index, int y_index, BufferedImage img, svm_model diamondModel) throws IOException {
		this.pos = Data.GetPos(x_index, y_index);
		this.index = new Point(x_index, y_index);
//		this.image = img.getRGB(pos.x, pos.y, size, size, null, 0, 1);
		this.image = img.getSubimage(this.pos.x, this.pos.y, size, size);
//		calCharacter();
		this.color = (int)svm.svm_predict(diamondModel, readFeaturesForSVM(this.image));
//		printDiamond();
//		this.color = color_list.indexOf(new Integer(this.character));
//		if(this.color == -1) {
//			color_list.add(new Integer(this.character));
//			this.color = color_list.size()-1;
//		}
	}
	
	public static void clearColors() {
		color_list.clear();
	}
	private void calCharacter() {
		this.character = this.image.getRGB(size/2, size/2);
//		System.out.println("The character of (" + index.x + "," + index.y +") is: " + this.character);
	}
	
	public int getCharacter() {
		return this.character;
	}
	
	public void printDiamond() {
		File f = new File("diamond"+ img_index++ +".jpg");
		try {
			ImageIO.write(image,"jpeg",f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static ArrayList<Integer> getColors() {
		return Diamond.color_list;
	}
	
	public svm_node[] readFeaturesForSVM(BufferedImage img) throws IOException {
		svm_node sampleNode[] = new svm_node[3 * Data.POINT_NUMBER_FOR_EACH_DATA];		
		for(int i = 0; i < Data.DATA_NUMBER_FOR_EACH_SAMPLE; i++) {
			Random random = new Random();
			Point randomPoint;
			int featureIndex = 0;
			for(int j = 0; j < Data.POINT_NUMBER_FOR_EACH_DATA; j++) {
				// generate that many random locations
				randomPoint = new Point(Data.SIZE_X / 4 + random.nextInt(Data.SIZE_X / 2),
						Data.SIZE_Y / 4 + random.nextInt(Data.SIZE_Y / 2)); 
				int rgb = img.getRGB(randomPoint.x, randomPoint.y);
				Color color = new Color(rgb);
				int features[] = {color.getRed(), color.getGreen(), color.getBlue()};
				for(int k = 0; k < 3; k++) {
					sampleNode[featureIndex] = new svm_node();
					sampleNode[featureIndex].index = featureIndex;
					sampleNode[featureIndex].value = features[k];
					featureIndex++;
				}
			}
		}
		
		return sampleNode;
	}

}
